///////////////////////////////////////////////////////////////////////////////
//
//  Copyright
//
///////////////////////////////////////////////////////////////////////////////

package ru.goodcore.display {

import flash.events.Event;

import flash.display.DisplayObject;
import flash.display.MovieClip;

/**
 * @author					BlooDHounD
 * @version					1.0
 * @playerversion			Flash 9
 * @langversion				3.0
 *
 * @keyword					movieclip, movieclipequivalent
 */
public class MovieClipEquivalent extends MovieClip {

	//-------------------------------------------------------------------------
	//
	//  Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor
	 */
	public function MovieClipEquivalent() {
		super();
	}

	//-------------------------------------------------------------------------
	//
	//  Variables
	//
	//-------------------------------------------------------------------------

	private var _running:			Boolean = true;

	//-------------------------------------------------------------------------
	//
	//  Overridden properties: MovieClip
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 */
	protected var _totalFrames:		int = 0;

	/**
	 * @private
	 */
	public override function get totalFrames():int {
		return this._totalFrames;
	}

	/**
	 * @private
	 */
	protected var _currentFrame:	int = 0;

	/**
	 * @private
	 */
	public override function get currentFrame():int {
		return this._currentFrame;
	}

	/**
	 * @private
	 */
	protected function setCurrentFrame(value:int):void {
		this._currentFrame = value;
	}

	/**
	 * @private
	 */
	public override function get framesLoaded():int {
		return this._totalFrames;
	} 

	//-------------------------------------------------------------------------
	//
	//  Overridedn methods: MovieClip
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 */
	public override function play():void {
		this._running = true;
		super.addEventListener( Event.ENTER_FRAME, this.handler_enterFrame, false, int.MAX_VALUE );
	}

	/**
	 * @private
	 */
	public override function stop():void {
		this._running = false;
		super.removeEventListener( Event.ENTER_FRAME, this.handler_enterFrame );
	}

	/**
	 * @private
	 */
	public override function nextFrame():void {
		if ( this._currentFrame < this._totalFrames ) {
			this.setCurrentFrame( this._currentFrame + 1 );
		}
	}

	/**
	 * @private
	 */
	public override function prevFrame():void {
		if ( this._currentFrame > 1 ) {
			this.setCurrentFrame( this._currentFrame - 1 );
		}
	}

	/**
	 * @private
	 */
	public override function gotoAndPlay(frame:Object, scene:String=null):void {
		var f:int = int( Number( frame ) );
		if ( this._currentFrame != f && f > 0 && f <= this._totalFrames ) {
			this.setCurrentFrame( f );
			if ( !this._running ) this.play();
		}
	}

	/**
	 * @private
	 */
	public override function gotoAndStop(frame:Object, scene:String=null):void {
		var f:int = int( Number(frame) );
		if ( this._currentFrame != f && f > 0 && f <= this._totalFrames ) {
			this.setCurrentFrame( f );
			if ( this._running ) this.stop();
		}
	}

	public override function addFrameScript(...args):void {
		
	}

	//-------------------------------------------------------------------------
	//
	//  Event handlers
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 */
	private function handler_enterFrame(event:Event):void {
		this.setCurrentFrame( this._currentFrame == this._totalFrames ? 1 : this._currentFrame + 1 );
	}

}
}